#include <iostream>
using namespace std;

int main() 
{

    int num; cin >> num;

    while(num--)
    {

        int n, k, score = 0, len = 0; cin >> n >> k;
        while(n--)
        {
            char number; cin >> number;
            if(number == 'W')
            {
                len++;
                if(len >= 3) score += k;
                else score += 1;
            }
            else 
            {
                len = 0;
                score -= 1;
            }
        }

        cout << score << endl;
    }

    return 0;    
}
// 64 位输出请用 printf("%lld")